package com.wx.fruits;

import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.wx.common.WxUser;
import com.wx.fruits.interceptor.SubscribeInterceptor;
import com.wx.fruits.interceptor.UnsubscribeInterceptor;
import com.wx.router.BaseAllRouterCreater;
import com.wx.router.interceptor.WxLogInterceptor;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageInterceptor;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpMessageRouterRule;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;

public class FruitsRouterCreater extends BaseAllRouterCreater {

	
	@Override
	public WxMpMessageRouter creater() {
		this.ruleLocationEnable();
		return super.creater();
	}

	public FruitsRouterCreater(WxMpMessageRouter router) {
		super(router);
	}

	@Override
	public void dealSubscribe(WxMpMessageRouterRule rule) {
		rule.interceptor(new WxLogInterceptor()).interceptor(new SubscribeInterceptor());
	}

	@Override
	public void dealUnsubscribe(WxMpMessageRouterRule rule) {
		rule.interceptor(new WxLogInterceptor()).interceptor(new UnsubscribeInterceptor());
	}

	@Override
	public void dealScanQrCodeUnsubscribe(WxMpMessageRouterRule rule) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dealScanQrCodeSubscribe(WxMpMessageRouterRule rule) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dealLocationEnable(WxMpMessageRouterRule rule) {
		rule.interceptor(new WxLogInterceptor()).interceptor(new WxMpMessageInterceptor() {
			
			@Override
			public boolean intercept(WxMpXmlMessage wxMessage,
					Map<String, Object> context, WxMpService wxMpService,
					WxSessionManager sessionManager) throws WxErrorException {
				//记录用户当前位置
				Double locationX = wxMessage.getLocationX();
				Double locationY = wxMessage.getLocationY();
				String openid = wxMessage.getFromUserName();
				String sql = "update sys_wx_user set longtitude = ? and latitude = ? where openid = ?";
				Db.update(sql,locationX,locationY,openid); 
				return true;
			}
		});
		

	}

	@Override
	public void dealMenuKey(WxMpMessageRouterRule rule) {

	}

	@Override
	public void dealMenuUrl(WxMpMessageRouterRule rule) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dealTxtMsg(WxMpMessageRouterRule rule) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dealImageMsg(WxMpMessageRouterRule rule) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dealVoiceMsg(WxMpMessageRouterRule rule) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dealVideoMsg(WxMpMessageRouterRule rule) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dealShortVideoMsg(WxMpMessageRouterRule rule) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dealLocationMsg(WxMpMessageRouterRule rule) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dealLinkMsg(WxMpMessageRouterRule rule) {
		// TODO Auto-generated method stub

	}

}
